﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TongCheng.API.OpenAPI.Util;

namespace TongCheng.API.OpenAPI.Request
{
    [Serializable]
    public class SceneryRequestHeader 
    {
        public SceneryRequestHeader()
        {
            this.Version = ConfigurationManager.AppSettings["Version"];
            this.ServerUrl = ConfigurationManager.AppSettings["SceneryServerUrl"];
            this.AccountID = ConfigurationManager.AppSettings["AccountID"];
            this.AccountKey = ConfigurationManager.AppSettings["AccountKey"];
            this.ReqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }

        [XmlElement("version")]
        public string Version { get; set; }
        [XmlElement("accountID")]
        public string AccountID { get; set; }
        [XmlElement("serviceName")]
        public string ServiceName { get; set; }
        [XmlElement("digitalSign")]
        public string DigitalSign { get; set; }
        [XmlElement("reqTime")]
        public string ReqTime { get; set; }
        [XmlIgnore]
        public string AccountKey { get; set; }

        [XmlIgnore]
        public string ServerUrl { get; set; }

        /// <summary>
        /// 提前验证参数。
        /// </summary>
        public void Validate()
        {
            if (string.IsNullOrEmpty(ServerUrl))
            {
                throw new TcException("10000", "ServerUrl缺少");
            }
            if (string.IsNullOrEmpty(AccountID))
            {
                throw new TcException("10001", "AccountID缺少");
            }
            if (string.IsNullOrEmpty(ServiceName))
            {
                throw new TcException("10002", "ServiceName缺少");
            }
            if (string.IsNullOrEmpty(AccountKey))
            {
                throw new TcException("10003", "AccountKey缺少");
            }


            String[] originalArray = { "Version=" + Version, "AccountID=" + AccountID, "ServiceName=" + ServiceName, "ReqTime=" + ReqTime };
            String[] sortedArray = Encryption.BubbleSort(originalArray);
            DigitalSign = Encryption.GetMD5ByArray(sortedArray, AccountKey, "utf-8");
        }

        ///<summary> 
        /// 签名算法[节选]
        ///</summary> 
        ///<param name="htValidationParams">参数</param> 
        ///<returns>true, 通过签名校验； false，未通过签名校验</returns> 
        private bool ValidateDigitalSign(Hashtable htValidationParams)
        {
            if (!htValidationParams.Contains("AccountKey") || String.IsNullOrWhiteSpace(htValidationParams["AccountKey"].ToString()))
            {
                throw new ApplicationException("缺少API帐户密钥");
            }
            //API帐户密钥 
            string accountKey = htValidationParams["AccountKey"].ToString().Trim();
            bool flag = false;
            List<string> stringList = new List<string>();
            if (htValidationParams.Contains("Version"))
                stringList.Add(string.Format("Version={0}", htValidationParams["Version"].ToString()));
            if (htValidationParams.Contains("AccountID"))
                stringList.Add(string.Format("AccountID={0}", Guid.Parse(htValidationParams["AccountID"].ToString())));
            if (htValidationParams.Contains("ServiceName"))
                stringList.Add(string.Format("ServiceName={0}", htValidationParams["ServiceName"].ToString()));
            if (htValidationParams.Contains("ReqTime") && htValidationParams["ReqTime"] != null)
                stringList.Add(string.Format("ReqTime={0}", ((DateTime)htValidationParams["ReqTime"]).ToString("yyyy-MM-dd HH:mm:ss.fff")));

            string[] originalArray = stringList.ToArray();
            string[] sortedArray = Encryption.BubbleSort(originalArray);
            string otherDigitalSign = Encryption.GetMD5ByArray(sortedArray, accountKey, "utf-8");

            if (otherDigitalSign.Equals(htValidationParams["DigitalSign"].ToString(), StringComparison.InvariantCultureIgnoreCase))
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }
    
    }
}
